<?php

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

// 参数通用模型
class mailerModule extends Model
{

    public function sendMail($title, $content, array $sendMailer = [])
    {
        $mail = new PHPMailer(true);
        $config = $GLOBALS['config_account'];
        try {
            $mail->SMTPDebug = 0;                                       // Enable verbose debug output
            $mail->isSMTP();                                            // Set mailer to use SMTP
            $mail->Host = $config['SMTP_SERVER'];                       //  'smtpdm.aliyun.com'; //  $config['SMTP_SERVER'];//'smtp1.example.com;smtp2.example.com';  // Specify main and backup SMTP servers
            $mail->SMTPAuth = true;                                     // Enable SMTP authentication
            $mail->Username = $config['SMTP_SEND_EMAIL'];               // 'servers@cdsile.com';//   $config['SMTP_SEND_EMAIL']; // 'user@example.com';                     // SMTP username
            $mail->Password = $config['SMTP_SEND_PASSWORD'];            //  'CDsile2017';  //$config['SMTP_SEND_PASSWORD'];//'secret';                               // SMTP password
            $mail->SMTPSecure = 'tls';                                  // Enable TLS encryption, `ssl` also accepted
            $mail->Port = 25;                                           //$config['SMTP_PORT']; //587;                                    // TCP port to connect to
            $mail->CharSet = 'UTF-8';
            $mail->setFrom($config['SMTP_SEND_EMAIL'], $config['SMTP_SEND_NICKNAME']);
            if (!empty($sendMailer)) {
                foreach ($sendMailer as $key => $val) {
                    $mail->addAddress($val);                            // Add a recipient
                }
            } else {
                $sendemails = $GLOBALS['config_cache']['SendEmails'];
                if ($sendemails) {
                    $arr = explode(';', $sendemails);
                    foreach ($arr as $key => $val) {
                        $mail->addAddress($val);                        // Add a recipient
                    }
                } else {
                    // 发送目标账号没添加
                    return false;
                }
            }
            $mail->isHTML(true);                               // Set email format to HTML
            $mail->Subject = $title;
            $mail->Body = $content;
            $mail->send();
        } catch (Exception $e) {
            echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
        }
    }

}